﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sisocana.Core.Models;
using Sisocana.Core.Interfaces;
using Sisocana.Core;

namespace SisocanaFronEnd.Controllers
{
    public class SearchPersonController : SiteController
    {
        private IPersonaService _personaService;

        public SearchPersonController()
            : this(new PersonaService())
        {
        }

        public SearchPersonController(IPersonaService personaService)
        {
            _personaService = personaService;
        }
        //
        // GET: /SearchPerson/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /SearchPerson/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /SearchPerson/SearchPersonResults

        public ActionResult SearchPersonResults(SearchPersonViewModel model)
        {
            SiteModel sit = ViewBag.Site;

            if (model == null)
            {
                model = new SearchPersonViewModel();
            }

            int idSitio = ViewBag.Site.id;
            if (ModelState.IsValid)
            {
                if (model.IsAdvanced)
                    model.results = _personaService.SearchPersonAdvanced(idSitio, model.FirstName, model.LastName, model.Phone, model.FromDateMissing, model.ToDateMissing);
                else
                    model.results = _personaService.SearchPersonByKeyword(idSitio, model.Keyword);
            }
            else
                model.results = new List<PersonaDesaparecidaFullModel>();
            foreach (var p in model.results)
            {
                if (p.foto==null)
                    p.foto = new DataImagen();
            }
            return View(model);
        }

        public ActionResult CambiarSearchForm(SearchPersonViewModel model)
        {
            model.IsAdvanced = !model.IsAdvanced;
            model.results = null;
            return RedirectToAction("SearchPersonResults", model);
        }

        //
        // POST: /SearchPerson/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SearchPerson/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /SearchPerson/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
